import {observable, extendObservable, action, toJS} from 'mobx';
import {
  getBlock,
  getUnitInfo,
  removePlan,
  deleteUnitInfo,
  addUnitInfo,
  updateDoorPwd,
  pushDoorPwd,
} from 'util/api';
import _ from 'lodash';
import {validateFields, getParams} from '../Common.js';
import {message} from 'antd';
import store from 'store';

//导出
export default class AddUnit {
  constructor() {
    this.reset(true);
  }

  @action
  reset = init => {
    const state = {
      fields: {
        block: {
          value: '',
          require: true,
        },
        unit: {value: '', require: true},
        password: {value: '', require: true},
      },
      addUnitVisible: false,
      type: 'new',
      currentData: {},
    };

    if (init) {
      extendObservable(this, state);
    } else {
      Object.keys(state).forEach(key => (this[key] = state[key]));
    }

    this.location = {};
    this.match = {};
    this.history = {};
  };

  //取消新增单元
  @action
  resetFields = async () => {
    this.fields = {
      block: {
        value: '',
        require: true,
      },
      unit: {value: '', require: true},
      password: {value: '', require: true},
    };
  };

  @action
  addUnitOk = async cancle => {
    console.log(toJS(this.fields));
    //校验字段
    validateFields(
      toJS(this.fields),
      async fields => {
        let params = getParams(fields);

        let res = await addUnitInfo(params);
        if (res.result === 'success') {
          this.resetFields();
          cancle();
          message.success(res.msg);
          let blockId = store.HouseholdStore.currentBlockId;
          store.HouseholdStore.getUnitInfo({blockId});
        }
      },
      newFields => {
        this.fields = newFields;
      },
    );
  };
  //改变字段
  @action
  handleFormChange = async changedFields => {
    const key = _.keys(changedFields);
    //showError让自己校验字段;
    const obj = {};
    obj[key] = {showError: false, ...changedFields[key]};
    this.fields = {...toJS(this.fields), ...obj};
  };
  //显示新增单元删除
  @action
  addtionUnit = async () => {
    //清空fields
    this.resetFields();
    store.CommonModalStore.confim('new');
  };
  @action
  changePassword = async cancle => {
    validateFields(
      toJS(this.fields),
      async fields => {
        let params = getParams(fields);
        params['unitId'] = this.currentData['unitId'];

        let res = await updateDoorPwd(params);
        if (res.result === 'success') {
          this.resetFields();
          cancle();
          message.success(res.msg);
          let blockId = store.HouseholdStore.currentBlockId;
          store.HouseholdStore.getUnitInfo({blockId});
        }
      },
      newFields => {
        this.fields = newFields;
      },
    );
  };
  @action
  onEdit = async record => {
    this.resetFields();
    let obj = toJS(this.fields);
    obj['block'].value = record.block || '';
    obj['unit'].value = record.unit || '';
    obj['password'].value = record.doorPwd | '';
    this.currentData = record;
    this.fields = obj;
    store.CommonModalStore.confim('edit');
  };
}
